//
//  RemindDrugManager.m
//  安好
//
//  Created by 冯凯 on 14-5-15.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

//#import "RemindManager.h"

#define KLocal_own_numeber          @"Local own numeber"
#define KUserNameAndMedicineName    @"userNameAndMedicineName"  //病人名 和 药名
#define KRemindID                   @"KRemindID"                //提醒id
#define KUserIDForLocal             @"userID"                   //用户id
#define KFid                        @"fidID"                   //用户id
#define KFamilyInfoDic              @"FamilyInfoDic"
#define KReminderTime               @"ReminderTime"
#import "RemindDrugManager.h"

@implementation RemindDrugManager
{
}

+ (RemindDrugManager *)shareInstance {
    static RemindDrugManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[RemindDrugManager alloc] init];
    });
    return instance;
}

- (id)init
{
    self = [super init];
    if (self) {
    }
    return self;
}


/**
 *  用药提醒--这里把UserName传
 *
 *  @return <#return value description#>
 */
//增加一条提醒
- (void)addNewRemindWithUserName:(NSString *)userName   // 病人名字
                 andMedicineName:(NSString *)medicName  // 药名
               andMedicineDosage:(NSString *)dosage     // 用药剂量
                      andNewTime:(NSString  *)timeDateArr// 提醒时间数组
                      andMessage:(NSString *)message    // 提醒内容
                       andUserID:(NSString *)userID     // 用户id
                     andRemindID:(NSString *)remindID andFamilyInfoDic:(NSDictionary *)familyInfoDic{ // 提醒ID
    
    //提醒内容: 1.服用者 2.服用药物 3.服用剂量 4.自定义内容
    NSString *notiMessage = [NSString stringWithFormat:@"安好小秘书提醒：用药时间到啦，为了我们的身体健康，要按时服药哦!"];
    //
    NSMutableDictionary *infoDic = nil;
    NSString *userMedicTime = nil;
    
    //    for (int i = 0; i < timeDateArr.count; i++) {
    
    //        NSString *dateStr = [timeDateArr objectAtIndex:i];
    
    NSString *dateStr = timeDateArr;
    
    NSDate *date = [self turnString:dateStr];
    
    //病人名，药名，时间，字符串
    userMedicTime = [NSString stringWithFormat:@"%@", dateStr];
    //        [NSString stringWithFormat:@"%@%@%@", userName, medicName, dateStr];
    
    infoDic = [[NSMutableDictionary alloc] init];
    /**
     *  把家人的信息字典接收到
     */
    [infoDic setObject:familyInfoDic forKey:KFamilyInfoDic];
    [infoDic setObject:userMedicTime forKey:KUserNameAndMedicineName];
    [infoDic setObject:userID forKey:KUserIDForLocal]; //病人id
    [infoDic setObject:remindID forKey:KRemindID]; //提醒id 唯一标示符
    [infoDic setObject:message forKey:KFid]; //提醒id 唯一标示符
    [infoDic setObject:timeDateArr forKey:KReminderTime];
    UILocalNotification *remindNotify = [[UILocalNotification alloc] init];
    remindNotify.fireDate = date;
    remindNotify.alertBody = notiMessage;
    remindNotify.timeZone = [NSTimeZone systemTimeZone];
    remindNotify.repeatInterval = kCFCalendarUnitDay;
    remindNotify.applicationIconBadgeNumber += 1;
    if ([message length] == 0) {
        remindNotify.soundName =  @"11.mp3";
    }else{
        remindNotify.soundName = message;
    }
    remindNotify.alertAction = @"服药提醒！";
    remindNotify.userInfo = infoDic;
    
    //添加到本地通知队列
    [[UIApplication sharedApplication] scheduleLocalNotification:remindNotify];
    //    }
    //    //NSLog(@"[UIApplication sharedApplication] notifys adds --->>%@",
    //          [[UIApplication sharedApplication] scheduledLocalNotifications]);
}



//删除一条提醒
- (void)removeRemindWithRemindID:(NSString *)remindID {
    
    NSMutableArray *localsMutableArr =
    [NSMutableArray arrayWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]];
    NSString* remID = remindID;
    
    //遍历找到当前病人所有数组
    for (NSInteger i = 0; i < localsMutableArr.count; i++) {
        UILocalNotification *localNoti = [localsMutableArr objectAtIndex:i];
        NSString * localID = [localNoti.userInfo objectForKey:KRemindID];
        
        if ([remID isEqualToString:localID]) {
            [[UIApplication sharedApplication] cancelLocalNotification:localNoti];
            [localsMutableArr removeObject:localNoti];
        }
    }
//    NSLog(@"[UIApplication sharedApplication] notifys delete --->>%@",
//          [[UIApplication sharedApplication] scheduledLocalNotifications]);
}


//找到某人的所有提醒
- (NSMutableArray *)getAllRemindWithRemindID:(NSString *)remindID {
    
    NSMutableArray *allRemindsArr = [[NSMutableArray alloc] init];
    
    NSMutableArray *localsMutableArr =
    [NSMutableArray arrayWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]];
    
    NSInteger remID = [remindID integerValue];//因为实际类型为NSNumber类型
    
    //遍历找到当前病人所有数组
    for (NSInteger i = 0; i < localsMutableArr.count; i++) {
        UILocalNotification *localNoti = [localsMutableArr objectAtIndex:i];
        NSInteger localID = [[localNoti.userInfo objectForKey:KRemindID] integerValue];
        
        if (remID == localID) {
            i -= 1;
            [allRemindsArr addObject:localNoti];
            [localsMutableArr removeObject:localNoti];
        }
    }
    return allRemindsArr;
}

- (NSMutableArray *)getAllRemindIDsWithUserID:(NSString *)userID {
    NSMutableArray *remindIDs = [[NSMutableArray alloc] init];
    
    NSMutableArray *localsMutableArr =
    [NSMutableArray arrayWithArray:[[UIApplication sharedApplication] scheduledLocalNotifications]];
    
    NSInteger uID = [userID integerValue];//因为实际类型为NSNumber类型
    
    //遍历找到当前病人所有数组
    for (NSInteger i = 0; i < localsMutableArr.count; i++) {
        UILocalNotification *localNoti = [localsMutableArr objectAtIndex:i];
        NSInteger localUid = [[localNoti.userInfo objectForKey:KUserIDForLocal] integerValue];
        
        if (uID == localUid) {
            i -= 1;
            [remindIDs addObject:[localNoti.userInfo objectForKey:KRemindID]];
            [localsMutableArr removeObject:localNoti];
        }
    }
    
    return remindIDs;
}

//把字符串转换成时间戳
- (NSDate *)turnString:(NSString *)timeString {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    NSDate *date = [dateFormatter dateFromString:timeString];
    return date;//要用国际标准时间所以不转换
}


////增加一条提醒
//- (void)addNewRemindWithUserName:(NSString *)userName   //  病人名字
//                 andMedicineName:(NSString *)medicName
//               andMedicineDosage:(NSString *)dosage
//                      andNewTime:(NSArray  *)timeDateArr
//                      andMessage:(NSString *)message
//                       andUserID:(NSString *)userID {
//
//    //提醒内容  服用者 服用药物 服用剂量 自定义内容
//    NSString *notiMessage = [NSString stringWithFormat:@"服用者:%@\n服用药物:%@\n剂量:%@\n提醒内容:%@\n", userName, medicName, dosage, message];
//
//    NSMutableDictionary *infoDic = nil;
//    NSString *userMedicTime = nil;
//
//    for (int i = 0; i < timeDateArr.count; i++) {
//
//        NSString *dateStr = [timeDateArr objectAtIndex:i];
//        NSDate *date = [self turnString:dateStr];
//
//        userMedicTime = [NSString stringWithFormat:@"%@%@%@", userName, medicName, dateStr];
//
//        infoDic = [[NSMutableDictionary alloc] init];
//        [infoDic setObject:userMedicTime forKey:KUserNameAndMedicineName];//唯一标识
//        [infoDic setObject:userID forKey:KUserIDForLocal];//病人id
//
//        UILocalNotification *remindNotify = [[UILocalNotification alloc] init];
//
//        remindNotify.fireDate = date;
//
//        remindNotify.alertBody = notiMessage;
//
//        remindNotify.timeZone = [NSTimeZone systemTimeZone];
//
//        remindNotify.repeatInterval = kCFCalendarUnitDay;
//
//        remindNotify.applicationIconBadgeNumber += 1;
//
//        remindNotify.soundName = UILocalNotificationDefaultSoundName;
//
//        remindNotify.alertAction = @"服药提醒！";
//
//        remindNotify.userInfo = infoDic;
//
//        //添加到本地通知队列
//        [[UIApplication sharedApplication] scheduleLocalNotification:remindNotify];
//    }
//    //NSLog(@"[UIApplication sharedApplication] notifys --->>%@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
//
//}
//
//
//
////删除一条提醒
//- (void)deleteRemindWithLocalNotifyArr:(NSArray *)localArr {
//    for (UILocalNotification *localNotify in localArr) {
//        [[UIApplication sharedApplication] cancelLocalNotification:localNotify];
//    }
//    //NSLog(@"[UIApplication sharedApplication] notifys --->>%@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
//}
//
//
//
////找到某人的所有提醒(按时间数组排序后的本地通知数组)
//- (NSMutableArray *)getAllRemindWithUserName:(NSString *)userName
//                             andMedicineName:(NSString *)medicName
//                                  andNewTime:(NSArray  *)timeDateArr {
//
//
//    NSArray *localsArr = [[UIApplication sharedApplication] scheduledLocalNotifications];
//    NSMutableArray *localsMutableArr = [NSMutableArray arrayWithArray:localsArr];
//    NSMutableArray *allRemindsArr = [[NSMutableArray alloc] initWithCapacity:timeDateArr.count];
//
//    NSString *userMedicTime = nil;
//
//    for (int j = 0; j < timeDateArr.count; j++) {
//        NSString *remindTimeStr = [timeDateArr objectAtIndex:j];
//
//        userMedicTime = [NSString stringWithFormat:@"%@%@%@", userName, medicName, remindTimeStr];
//
//        //遍历找到当前病人所有数组
//        for (int i = 0; i < localsMutableArr.count; i++) {
//            UILocalNotification *localNoti = [localsMutableArr objectAtIndex:i];
//            NSString *userNameDrugName = [localNoti.userInfo objectForKey:KUserNameAndMedicineName];
//
//            if ([userMedicTime isEqualToString:userNameDrugName]) {
//
//                [allRemindsArr addObject:localNoti];
//                [localsMutableArr removeObject:localNoti];
//            }
//        }
//    }
//
//    return allRemindsArr;
//}
//
//
////把字符串转换成时间戳
//- (NSDate *)turnString:(NSString *)timeString
//{
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//
//    [dateFormatter setDateFormat:@"HH:mm"];
//
//    NSDate *date = [dateFormatter dateFromString:timeString];
//
////    NSTimeZone *zone = [NSTimeZone systemTimeZone];
////
////    NSInteger interval = [zone secondsFromGMTForDate:date];
////
////    NSDate *localeDate = [date dateByAddingTimeInterval:interval];//获取本地时间
//
//    return date;//要用国际标准时间所以不转换
//}
@end
